require(ggplot2)
require(foreign)
require(reshape2)
require(car)
require(RItools)
require(Matching)
require(xtable)
require(grid)
library(colorspace)
library(plyr)

mypdf <- function(name, ...) {
  pdf(paste(name, format(Sys.Date(), "%y%m%d"), ".pdf", sep=""), ...)
}

#rep.dir <-
  "~/Dropbox/2-Shared/work/Collaborations/Dafoe/Honor/Replication-Final"
rep.dir <-
  "/Users/Allan/Dropbox/1Papers/1Reputation/Honor-Culture-War/2012-05/Replication-Final"




setwd(paste0(rep.dir, "/4-Analysis/save"))
P <- read.dta("Results150421-merged.dta")
P$global_p <- ifelse(P$global_p == 0, 1/1000, P$global_p)

summary(P)

table(P$cov_set)

P$cov_set <- recode(P$cov_set, "
'none' = 'unmatched';
'log.placebo' = 'lagged DVs';
'basic.covs' = 'basic covs.';
'struct.covs' = 'structural covs.';
'party.covs' = 'party covs.';
'inTerm.covs' = 'in-term covs.';
'full.covs' = 'all covs.'")

P$cov_set <- ordered(P$cov_set, levels = c("unmatched", "basic covs.",
                                    "lagged DVs", "structural covs.",
                                    "party covs.", "in-term covs.",
                                    "all covs."))


summary(P)

##'Bush out' = 'Bush n-S, Truman n-S';
P$SouthCoding <- Recode(P$SouthCoding, "
  'original' = 'Bush South, Truman non-South';
  'Truman in' = 'Bush South, Truman South';
  'Bush out' = 'Bush non-South, Truman non-South';
  'Bush out and Truman in' = 'Bush non-South, Truman South'")
P$MID_subset <- Recode(P$MID_subset, "'primary' = 'multiparty'")
P$MID_subset <- factor(P$MID_subset, levels=c("multiparty", "bilateral"))
P$SouthCoding <- ordered(P$SouthCoding,
                         c("Bush South, Truman non-South",
                           "Bush South, Truman South",
                           "Bush non-South, Truman non-South",
                           "Bush non-South, Truman South"))

table(P$test_stat)
P$test_stat <- Recode(P$test_stat, '"harmonic" = "wtd. mean"; "hlm" = "HGLM"')
P$test_stat <- ordered(P$test_stat, c("mean", "rank", "wtd. mean", "HGLM"))

P$matching <- ifelse(P$replace, "with replac.", "w/o replac.")
P$matching[is.na(P$matching)] <- "unmatched"
table(P$matching)

P$sub.mat <- interaction(P$MID_subset, paste("(", P$matching, ")", sep=""),
                         drop = TRUE, lex.order=TRUE, sep = " ")
table(P$sub.mat)

colours <- c("#504030", "#6F853A", "#660000", "#000066")

setwd(paste0(rep.dir, "/5-Figures"))

pf <- (ggplot(data = P, aes(x = sqrt(global_p), y=sub.mat, color = test_stat,
            shape = test_stat, color = matching))
 + geom_point()
 + facet_grid(cov_set ~ SouthCoding, scales = "free_y", space = "free", 
             labeller = function (var, val) {
               ifelse(val == "unmatched", "none", as.character(val))
             })
 + scale_x_continuous(breaks = sqrt(c(0, .01, .05, .1, .2, .3, .5, 1)),
                      labels = c("0", ".01", ".05", ".1", ".2", ".3", ".5", "1"),
                      limits = c(0, 1), expand=c(0,0))
 + scale_shape_manual(values = c(15, 20, 3, 4))
 + labs(y = NULL, x = "Global Significance Level (Square-Root Scale)",
        shape = "Test\nStatistic", color = "Test\nStatistic")
 + geom_vline(xintercept = sqrt(c(.01, .05, .1)), linetype = "dotted")
 + theme_bw()
 + theme(axis.title.x = element_text(size = 15),
         axis.text.y = element_text(size = 13, colour = "grey40"),
         axis.text.x = element_text(size = 9, colour = "grey40"),
         strip.text.x = element_text(size = 9),
         legend.position = "left")
 #Changing default colors
 + scale_color_manual(values = colours) 
 # +scale_color_brewer(palette = "Set1") 
 #AD: Removing minor grid lines
 + theme(panel.grid.minor=element_blank())
 #AD: Increasing spacing between facets. 
 + theme(panel.margin = unit(0.5, "lines"))
 #Removing size legend
 + guides(size=FALSE)
 )

mypdf("pPlot", width = 13, height = 8)
pf
dev.off()

wid <- 13
resol <- 200
tiff(file = paste("pPlot", format(Sys.Date(), "%y%m%d"), ".tiff", sep=""), 
     width = wid, height = wid*8/13, units = "in", res=resol)
pf
dev.off()

summary(P)
summary(subset(P, MID_subset == "bilateral"))
summary(subset(P, SouthCoding == "Bush South, Truman non-South"))
summary(subset(P, SouthCoding == "Bush South, Truman non-South" &
               MID_subset == "bilateral"))

summary(subset(P, test_stat == "mean"))
summary(subset(P, test_stat == "mean"
               & SouthCoding == "Bush South, Truman non-South"
               & MID_subset == "bilateral"))
summary(subset(P, test_stat == "wtd. mean"
               & SouthCoding == "Bush South, Truman non-South"
               & MID_subset == "bilateral"))

subset(arrange(P, SouthCoding, cov_set),
       SouthCoding == "Bush South, Truman non-South" & MID_subset == "bilateral"
       & cov_set != "unmatched" & test_stat == "mean",
       c(cov_set, ForceUS_p_adj:global_p))

P$Force <- paste0(signif(P$ForceUS_T0, 2), " (", round(P$ForceUS_p_adj, 2), ")")
P$Duration <- paste0(signif(P$DurationUS_T0, 3), " (",
                     round(P$DurationUS_p_adj, 2), ")")
P$Outcome <- paste0(signif(P$OutcomeUS_T0, 3), " (",
                    round(P$OutcomeUS_p_adj, 2), ")")
P$Global <- round(P$global_p, 2)

replace.res <- subset(arrange(P, cov_set),
                      SouthCoding == "Bush South, Truman non-South"
                      & MID_subset == "bilateral" & cov_set != "unmatched"
                      & test_stat == "wtd. mean" & replace == 1, Force:Global)
xtable(t(replace.res))

notreplace.res <- subset(arrange(P, cov_set),
                      SouthCoding == "Bush South, Truman non-South"
                      & MID_subset == "bilateral" & cov_set != "unmatched"
                      & test_stat == "wtd. mean" & replace == 0, Force:Global)
xtable(t(notreplace.res))



P$max_p_adj <- apply(subset(P,, ForceUS_p_adj:OutcomeUS_p_adj), 1, max)
P$max_p <- apply(subset(P,, ForceUS_p:OutcomeUS_p), 1, max)

pres <- matrix(data = c("Madison", "Monroe", "Jackson", "Tyler", "Polk", "Johnson, A.",
           "Wilson", 
  "Johnson, L.", "Carter", "Clinton", "Bush, G. W.", "Fillmore",
  "Roosevelt, T.", "Cleveland", "Lincoln", "Van Buren", "Pierce", "Grant",
  "Nixon", "Ford", "Reagan", "Eisenhower", "Fillmore", "Fillmore", "Lincoln",
  "Van Buren", "Van Buren", "Fillmore", "Roosevelt, F.", "Kennedy", "Reagan",
  "Ford", "Reagan", "Roosevelt, T.", "Grant", "Harding", "Buchanan", "Obama",
  "Truman", "Bush, G. H. W.", "Lincoln", "Ford", "Roosevelt, F.", "Nixon",
  "Roosevelt, T.", "Roosevelt, T.", "Roosevelt, F.", "Nixon", "Obama", "Truman",
  "Buchanan", "Bush, G. H. W.", "Roosevelt, F.", "Obama", "Nixon", "Fillmore",
  "Van Buren", "Roosevelt, F.", "Buchanan", "Pierce", "Arthur", "Roosevelt, T.",
  "Bush, G. H. W.", "Ford", "Reagan", "Obama", "Roosevelt, T.", "Roosevelt, F.",
  "Roosevelt, F.", "Buchanan", "Van Buren", "Arthur", "Buchanan",
  "Bush, G. H. W.", "Ford", "Obama", "Obama", "Arthur", "Roosevelt, T.",
  "Grant", "Buchanan", "Van Buren", "Lincoln", "Roosevelt, F.",
  "Bush, G. H. W.", "Ford", "Obama", "Nixon", "Van Buren", "Roosevelt, T.",
  "Roosevelt, F.", "Buchanan", "Van Buren", "Arthur", "Roosevelt, T.",
  "Kennedy", "Ford", "Obama", "Obama", "Arthur", "Roosevelt, T.", "Cleveland",
  "Buchanan", "Van Buren", "Grant", "Roosevelt, F.", "Kennedy", "Ford", "Obama",
  "Bush, G. H. W.", "Van Buren", "Roosevelt, T.", "Roosevelt, F.", "Buchanan",
  "Buchanan", "Cleveland", "Roosevelt, T.", "Kennedy", "Truman", "Obama",
  "Obama", "Grant", "Roosevelt, T.", "Coolidge", "Van Buren", "Cleveland",
  "Buchanan", "Roosevelt, F.", "Kennedy", "Ford", "Obama", "Reagan", "Obama",
  "Grant", "Roosevelt, F.", "Van Buren", "Van Buren", "Coolidge",
  "Roosevelt, F.", "Kennedy", "Ford", "Obama", "Reagan"), nrow = 11)


